#!/usr/bin/env python
# coding=utf-8
import requests
import re, time
from bs4 import BeautifulSoup
from urllib.request import urlretrieve


# 获得网址
def getUrls():
    imgurls = []
    i = 1
    while i < 187:
        url = 'http://comic.dragonballcn.com/list/gain_1.php?did=0-2-0&fpp=10&fid=' + str(i)
        imgurls.append(url)
        i += 1
    return imgurls


# 获得页面
def gethtml(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36'
    }
    res = requests.get(url, headers=headers)
    res.encoding = 'utf-8'
    html = res.text
    soup = BeautifulSoup(html, 'lxml')
    # print(soup.prettify())
    # print(soup.input.attrs['value'])  # 获得目标图片网址
    pic = soup.input.attrs['value']
    print(pic)
    pattern = re.compile('http://.*?01/(.*?)-.*?.jpg')
    picnum = re.findall(pattern, pic)  # 提取图片名字
    print(picnum)
    # print(picnum[0])
    urlretrieve(pic, './' + picnum[0] + '.jpg')


if __name__ == '__main__':
    for i in getUrls():
        # print(i)
        gethtml(i)
        time.sleep(3)
